package com.example.mous;

import android.os.Bundle;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

























public class MainActivity extends AppCompatActivity {
    private TouchPadView touchPadView;
    private EditText ipEditText, portEditText;
    private EditText inputEditText;
    private Button sendButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ipEditText = findViewById(R.id.ipEditText);
        portEditText = findViewById(R.id.portEditText);
        touchPadView = findViewById(R.id.touchPad);
        Button connectButton = findViewById(R.id.connectButton);
        inputEditText = findViewById(R.id.inputEditText);
        sendButton = findViewById(R.id.sendButton);
        sendButton.setOnClickListener(v -> sendTextToServer());
        inputEditText.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEND) {
                sendTextToServer();
                return true;
            }
            return false;
        });
        connectButton.setOnClickListener(v -> connectToServer());
    }
    private void sendTextToServer() {
        String text = inputEditText.getText().toString().trim();
        if (text.isEmpty()) return;
        new Thread(() -> {
            try {
                synchronized (touchPadView) {
                    if (touchPadView.isConnected()) {
                        DataOutputStream dos = touchPadView.getDataOutputStream();
                        dos.writeByte(5); // 文本命令
                        dos.writeUTF(text);
                        dos.flush();
                        runOnUiThread(() -> inputEditText.setText(""));
                    } else {
                        runOnUiThread(() ->
                                Toast.makeText(this, "未连接", Toast.LENGTH_SHORT).show());
                    }
                }
            } catch (IOException e) {
                runOnUiThread(() ->
                        Toast.makeText(this, "发送失败", Toast.LENGTH_SHORT).show());
            }
        }).start();
    }
    private void connectToServer() {
        new Thread(() -> {
            try {
                String ip = ipEditText.getText().toString();
                int port = Integer.parseInt(portEditText.getText().toString());
                Socket socket = new Socket();
                socket.connect(new InetSocketAddress(ip, port), 3000); // 3秒连接超时

                runOnUiThread(() -> {
                    try {
                        touchPadView.setSocket(socket);
                        Toast.makeText(this, "已连接", Toast.LENGTH_SHORT).show();
                    } catch (IOException e) {
                        Toast.makeText(this, "连接失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
            } catch (Exception e) {
                runOnUiThread(() ->
                        Toast.makeText(this, "连接失败: " + e.getMessage(), Toast.LENGTH_SHORT).show());
            }
        }).start();
    }
}